<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../libs/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="../libs/app.css"/>
    <link href="../css/main.css" rel="stylesheet"/>
    <link href="../css/login.css" rel="stylesheet"/>
</head>

<body>
<div class="head">
    <img src="../images/all-left.png" alt="" class="mui-action-back">
    <p>找回密码</p>
</div>
<div class="context">
    <div><input class="number" id="number" type="tel" maxlength="11" placeholder="请输入手机号码"><img src="../images/suo.png"
                                                                                                alt=""></div>
    <div><input class="password" id="password-1" type="password" placeholder="请输入6~18位密码"><img
            src="../images/photo-num.png"
            alt=""></div>
    <!--<div><input class="password" id="password-2" type="password" placeholder="请输入6~18位密码"><img-->
            <!--src="../images/photo-num.png"-->
            <!--alt=""></div>-->
    <div><input class="yan" type="tel" maxlength="6" id="yan" placeholder="请输入验证码">
        <img src="../images/yanzheng.png" alt="">
        <div class="button" id="button">
            <button id="button-button">获取验证码</button>
        </div>
    </div>
    <div>
        <button class="sign">提交</button>
    </div>
</div>
<script src="../libs/mui.min.js"></script>
<script src="../js/jquery-3.1.1.min.js"></script>
<script src="../js/url.js"></script>
<script type="text/javascript">
    mui.init();

    var checkAfterPhone = '';

    // 获取验证码
    var click = true;
    $('.button').on('tap', function () {
        if (click) {
            var hm = /^1[34578]\d{9}$/;
            var phone = $('#number').val();
            if (phone == '' || (!hm.test(phone))) {
                mui.alert('手机号码不正确', '提示');
            } else {
//	        	plus.nativeUI.showWaiting();
                $.ajax({
                    url: ur + "rest/appUserController/checkCode", //请求的url地址
                    dataType: "json",   //返回格式为json
                    async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                    data: {
                        "phone": phone,
                        "type": "password"
                    },    //参数值
                    type: "POST",   //请求方式
                    beforeSend: function () {
                        //请求前的处理
                    },
                    success: function (data) {
//	                	plus.nativeUI.closeWaiting();
                        if (data.meta.code == 'S002') {
                            checkAfterPhone = phone;
                            click = false;
                            mui.toast('验证消息已发送');
                            var ti = 60;
                            var time = setInterval(function () {
                                ti--;
                                $('#button-button').text(ti + '(s)');
                                $('#button-button').css('background', '#ccc');
                                if (ti == 1) {
                                    clearInterval(time);
                                    $('#button-button').css('background', '#f8bc02');
                                    $('#button-button').text('获取验证码');
                                    click = true;
                                }
                            }, 1000);
                        } else {
                            mui.toast('系统异常');
//                          console.log(JSON.stringify(data));
                        }
                    },
                    complete: function () {
//                      plus.nativeUI.closeWaiting();
                    },
                    error: function () {
//                      plus.nativeUI.closeWaiting();
                    }
                });
            }
        } else {

        }

    });


    // 点击提交 找回密码
    $('.sign').on('tap', function (event) {
        var phone = $('#number').val();
        var phone_reg = /^1[34578]\d{9}$/;
        var PW_reg = /^[u4E00-u9FA5]{6,18}$/; // 非中文密码
        if (phone == '' || (!phone_reg.test(phone)) || (checkAfterPhone !== phone)) {
            mui.alert('手机号码不正确', '提示');
        } else if (!PW_reg.test($('#password-1').val())) {
            mui.alert('请填写6~18位密码', '提示');
        } else if ($('#password-1').val() == '') {
            mui.alert('密码不能为空', '提示');
        } else if ($('#yan').val() == '') {
            mui.alert('请输入验证码', '提示');
        } else {
            // plus.nativeUI.showWaiting();
            $.ajax({
                url: ur + 'rest/appUserController/password/',
                type: 'POST',
                dataType: 'json',
                data: {
                    "phone": $('#number').val(),
                    "password": $('#password-1').val(),
                    "checkCode": $('#yan').val(),
                },
                /*contentType: "application/json",*/
            })
                .done(function (data) {
//                  console.log(JSON.stringify(data));
                    if (data.meta.code == 'E002') {
//                      plus.nativeUI.closeWaiting();
                        mui.toast(data.meta.message);

                    } else if (data.meta.code == 'S002') {
//                      plus.nativeUI.closeWaiting();
                        mui.toast('修改密码成功');

                    } else if (data.meta.code == 'E003') {
//                      plus.nativeUI.closeWaiting();
                        mui.toast(data.meta.message);
                    }

//                plus.nativeUI.closeWaiting();
                })
                .fail(function (data) {
//                  plus.nativeUI.closeWaiting();
//                  console.log(JSON.stringify(data));
                })
                .always(function (data) {
//                  console.log(JSON.stringify(data));
                    mui.back();
                })
        }
    })

</script>
</body>
</html>